﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    public class CastingRecordPageViewModel : ViewModelBase
    {

        #region 分页使用
        /// <summary>
        /// 每页数量
        /// </summary>
        private int itemCount = 20;

        /// <summary>
        /// 总数量
        /// </summary>
        private int totalDataCount = 0;

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageNumber
        {
            get
            {
                int totalPageNumber = totalDataCount / itemCount;
                if (totalDataCount % itemCount > 0)
                {
                    totalPageNumber++;
                }
                return totalPageNumber;

            }
        }
        /// <summary>
        /// 总数据量
        /// </summary>
        public int TotalDataCount
        {
            get { return totalDataCount; }
            set
            {
                totalDataCount = value;
                RaisePropertyChanged("TotalDataCount");
                RaisePropertyChanged("TotalPageNumber");
            }
        }

        /// <summary>
        /// 当前所在页数
        /// </summary>
        private int _CurrentPageNumber = 1;
        /// <summary>
        /// 当前所在页数
        /// </summary>
        public int CurrentPageNumber
        {
            get { return _CurrentPageNumber; }
            set
            {
                _CurrentPageNumber = value;
                RaisePropertyChanged("CurrentPageNumber");
            }
        }
        /// <summary>
        /// 上一页页数
        /// </summary>
        public int PreviousPageNumber
        {
            get
            {
                if (_CurrentPageNumber - 1 > 0)
                {
                    return _CurrentPageNumber - 1;
                }
                return 1;
            }

        }
        /// <summary>
        /// 下一页页数
        /// </summary>
        public int NextPageNumber
        {
            get
            {
                if (_CurrentPageNumber + 1 <= TotalPageNumber)
                {
                    return _CurrentPageNumber + 1;
                }
                return _CurrentPageNumber;
            }

        }
        /// <summary>
        /// 分页页码集合
        /// </summary>
        private ObservableCollection<PageNumerModel> _PageNumers = new ObservableCollection<PageNumerModel>();
        /// <summary>
        /// 分页页码集合
        /// </summary>
        public ObservableCollection<PageNumerModel> PageNumers
        {
            get => _PageNumers;
            set
            {
                _PageNumers = value;
                RaisePropertyChanged("PageNumers");
            }
        }
        #endregion 分页使用
        #region 声明命令

        /// <summary>
        /// 搜索命令
        /// </summary>
        public DelegateCommand SeachCommand { get; set; }
        /// <summary>
        /// 跳转到指定页面命令
        /// </summary>
        public DelegateCommand GoToPageByNumberCommand { get; set; }
        /// <summary>
        /// 跳转到指定页面并关闭AllPageNumberPanel窗体命令
        /// </summary>
        public DelegateCommand GoToPageByNumberAndCloseAllPageNumberPanelCommand { get; set; }

        /// <summary>
        /// 显示更改到指定页面命令
        /// </summary>
        public DelegateCommand ChangeCurrentPageNumberCommand { get; set; }
        /// <summary>
        /// 显示配方选择页面命令
        /// </summary>
        public DelegateCommand ShowSelectPageCommand { get; set; }
        /// <summary>
        /// 添加到配方条件命令
        /// </summary>
        public DelegateCommand AddCastingNumberConditionsCommand { get; set; }
        /// <summary>
        /// 添加到最近使用配方条件命令
        /// </summary>
        public DelegateCommand AddRecipeNameConditionsCommand { get; set; }
        /// <summary>
        /// 添加到工艺搜索条件
        /// </summary>
        public DelegateCommand AddMoldTechConditionsCommand { get; set; }

        /// <summary>
        /// 添加到类别搜索条件
        /// </summary>
        public DelegateCommand AddTypeConditionsCommand { get; set; }

        /// <summary>
        /// 选择配方命令
        /// </summary>
        public DelegateCommand SelectRecipeCommand { get; set; }


        /// <summary>
        /// 显示日历窗体命令
        /// </summary>
        public DelegateCommand ShowCalendarCommand { get; set; }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void InitCommand()
        {
            SelectRecipeCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SelectRecipeHandler)
            };


            ShowCalendarCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowCalendarHandler)
            };
            AddCastingNumberConditionsCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(AddCastingNumberConditionsHandler)
            };

            AddRecipeNameConditionsCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(AddRecipeNameConditionsHandler)
            };

            AddMoldTechConditionsCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(AddMoldTechConditionsHandler)
            };

            AddTypeConditionsCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(AddTypeConditionsHandler)
            };

            GoToPageByNumberCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(GoToPageByNumberHandler)
            };

            GoToPageByNumberAndCloseAllPageNumberPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(GoToPageByNumberAndCloseAllPageNumberPanelHandler)
            };

            ChangeCurrentPageNumberCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ChangeCurrentPageNumberHandler)
            };

            SeachCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(SeachHandler)
            };





        }

        /// <summary>
        /// 选择配方命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void SelectRecipeHandler(object obj)
        {
            if (obj == null)
            {
                MessageBox.Show("请在铸造记录列表中选择铸造记录", "警告");
                return;
            }
            Messenger.Default.Send<int>(int.Parse(obj.ToString()), "SetPreSelectRecipe");
            Messenger.Default.Send<string>("RecipeSelectionPage", "GoToPage");
        }


        /// <summary>
        /// 显示日历窗体命令处理方法
        /// </summary>
        /// <param name="obj"></param>
        private void ShowCalendarHandler(object obj)
        {
            WindowManager.ShowModalCalendar("CalendarWindow", obj.ToString());
        }



        /// <summary>
        /// 搜索命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void SeachHandler(object obj)
        {
            QueryDataCollection();
        }

        /// <summary>
        /// 打开更改页码窗体
        /// </summary>
        /// <param name="obj"></param>
        private void ChangeCurrentPageNumberHandler(object obj)
        {
            PageNumers = new ObservableCollection<PageNumerModel>();
            for (int i = 0; i < TotalPageNumber; i++)
            {
                PageNumers.Add(new PageNumerModel() { PageNumberStr = (i + 1).ToString() });
            }
            if (PageNumers.Count > 1)
            {
                WindowManager.ShowDialog("AllPageNumberPanelForCastingRecordWindow", "");
            }
        }

        /// <summary>
        /// 跳转到更改页码的列表
        /// </summary>
        /// <param name="obj"></param>
        private void GoToPageByNumberAndCloseAllPageNumberPanelHandler(object obj)
        {
            GoToPageByNumberHandler(obj);
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "HideAllPageNumberPanelForCastingRecord"));
        }


        /// <summary>
        /// 跳转到指定页面命令的处理器
        /// </summary>
        /// <param name="obj"></param>
        private void GoToPageByNumberHandler(object obj)
        {
            switch (obj.ToString())
            {
                case "PreviousPage":
                    CurrentPageNumber = PreviousPageNumber;
                    break;
                case "NextPage":
                    CurrentPageNumber = NextPageNumber;
                    break;
                case "FirstPage":
                    CurrentPageNumber = 1;
                    break;
                case "LastPage":
                    CurrentPageNumber = TotalPageNumber;
                    break;
                default:
                    CurrentPageNumber = int.Parse(obj.ToString());
                    break;
            }
            QueryDataCollection();
        }


        /// <summary>
        /// 添加或删除配方名称条件命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void AddCastingNumberConditionsHandler(object obj)
        {
            if (obj != null)
            {
                bool IsHave = false;
                for (int i = 0; i < CastingNumberConditions.Count; i++)
                {
                    if (CastingNumberConditions[i] == obj.ToString())
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (IsHave)
                {
                    CastingNumberConditions.Remove(obj.ToString());
                }
                else
                {
                    CastingNumberConditions.Add(obj.ToString());
                }
                QueryDataCollection();
            }
        }
        /// <summary>
        /// 添加或删除合金条件命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void AddRecipeNameConditionsHandler(object obj)
        {
            if (obj != null)
            {
                bool IsHave = false;

                for (int i = 0; i < RecipeNameConditions.Count; i++)
                {
                    if (RecipeNameConditions[i] == obj.ToString())
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (IsHave)
                {
                    RecipeNameConditions.Remove(obj.ToString());
                }
                else
                {
                    RecipeNameConditions.Add(obj.ToString());
                }
                QueryDataCollection();
            }
        }
        /// <summary>
        /// 添加或删除工艺条件命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void AddMoldTechConditionsHandler(object obj)
        {
            if (obj != null)
            {
                bool IsHave = false;

                for (int i = 0; i < MoldTechConditions.Count; i++)
                {
                    if (MoldTechConditions[i] == obj.ToString())
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (IsHave)
                {
                    MoldTechConditions.Remove(obj.ToString());
                }
                else
                {
                    MoldTechConditions.Add(obj.ToString());
                }
                QueryDataCollection();
            }
        }
        /// <summary>
        /// 添加或删除直径条件命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void AddTypeConditionsHandler(object obj)
        {
            if (obj != null)
            {
                bool IsHave = false;

                for (int i = 0; i < TypeConditions.Count; i++)
                {
                    if (TypeConditions[i] == obj.ToString())
                    {
                        IsHave = true;
                        break;
                    }
                }
                if (IsHave)
                {
                    TypeConditions.Remove(obj.ToString());
                }
                else
                {
                    TypeConditions.Add(obj.ToString());
                }
                QueryDataCollection();
            }
        }
        #endregion 声明命令

        #region 声明搜索条件集合
        /// <summary>
        /// 搜索条件铸造编号
        /// </summary>
        private string _CastNumber;
        /// <summary>
        /// 搜索条件铸造编号
        /// </summary>
        public string CastNumber
        {
            get { return _CastNumber; }
            set
            {
                _CastNumber = value;
                RaisePropertyChanged("CastNumber");
            }
        }


        /// <summary>
        /// 铸造编号搜索条件
        /// </summary>
        private List<string> _CastingNumberConditions = new List<string>();
        /// <summary>
        /// 铸造编号搜索条件
        /// </summary>
        public List<string> CastingNumberConditions
        {
            get { return _CastingNumberConditions; }
            set
            {
                _CastingNumberConditions = value;
                RaisePropertyChanged("CastingNumberConditions");
            }
        }

        /// <summary>
        /// 配方名称搜索条件
        /// </summary>
        private List<string> _RecipeNameConditions = new List<string>();
        /// <summary>
        /// 配方名称条件
        /// </summary>
        public List<string> RecipeNameConditions
        {
            get { return _RecipeNameConditions; }
            set
            {
                _RecipeNameConditions = value;
                RaisePropertyChanged("RecipeNameConditions");
            }
        }
        /// <summary>
        /// 工艺搜索条件
        /// </summary>
        private List<string> _MoldTechConditions = new List<string>();
        /// <summary>
        /// 工艺搜索条件
        /// </summary>
        public List<string> MoldTechConditions
        {
            get { return _MoldTechConditions; }
            set
            {
                _MoldTechConditions = value;
                RaisePropertyChanged("MoldTechConditions");
            }
        }
        /// <summary>
        /// 类别搜索条件
        /// </summary>
        private List<string> _TypeConditions = new List<string>();
        /// <summary>
        /// 类别搜索条件
        /// </summary>
        public List<string> TypeConditions
        {
            get { return _TypeConditions; }
            set
            {
                _TypeConditions = value;
                RaisePropertyChanged("TypeConditions");
            }
        }
        #endregion 声明搜索条件集合
        /// <summary>
        /// 当前搜索结果集合
        /// </summary>
        private ObservableCollection<RecipeTable> _DataCollection = new ObservableCollection<RecipeTable>();
        /// <summary>
        /// 当前搜索结果集合
        /// </summary>
        public ObservableCollection<RecipeTable> DataCollection
        {
            get { return _DataCollection; }
            set
            {
                _DataCollection = value;
                RaisePropertyChanged("DataCollection");
            }
        }

        /// <summary>
        /// 当前选中的配方
        /// </summary>
        private RecipeTable _SelectRecipeModel = new RecipeTable();
        /// <summary>
        /// 当前选中的配方
        /// </summary>
        public RecipeTable SelectRecipeModel
        {
            get { return _SelectRecipeModel; }
            set
            {
                _SelectRecipeModel = value;
                RaisePropertyChanged("SelectRecipeModel");
            }
        }



        #region 快速筛选搜索条件的数据
        /// <summary>
        /// 最近使用的铸造编号列表
        /// </summary>
        private ObservableCollection<RecipeTable> _LastUsedCastingNumbers = new ObservableCollection<RecipeTable>();
        /// <summary>
        /// 最近使用的铸造编号列表
        /// </summary>
        public ObservableCollection<RecipeTable> LastUsedCastingNumbers
        {
            get { return _LastUsedCastingNumbers; }
            set
            {
                _LastUsedCastingNumbers = value;
                RaisePropertyChanged("LastUsedCastingNumbers");
            }
        }

        /// <summary>
        /// 配方名称条件列表
        /// </summary>
        private ObservableCollection<RecipeTable> _RecentlyUsedRecipes = new ObservableCollection<RecipeTable>();
        /// <summary>
        /// 配方名称条件列表
        /// </summary>
        public ObservableCollection<RecipeTable> RecentlyUsedRecipes
        {
            get { return _RecentlyUsedRecipes; }
            set
            {
                _RecentlyUsedRecipes = value;
                RaisePropertyChanged("RecentlyUsedRecipes");
            }
        }

        /// <summary>
        /// 工艺条件列表
        /// </summary>
        private ObservableCollection<RecipeTable> _MoldTechs = new ObservableCollection<RecipeTable>();
        /// <summary>
        /// 工艺条件列表
        /// </summary>
        public ObservableCollection<RecipeTable> MoldTechs
        {
            get { return _MoldTechs; }
            set
            {
                _MoldTechs = value;
                RaisePropertyChanged("MoldTechs");
            }
        }
        /// <summary>
        /// 搜索条件开始时间
        /// </summary>
        private string _StartTime;
        /// <summary>
        /// 搜索条件开始时间
        /// </summary>
        public string StartTime
        {
            get { return _StartTime; }
            set
            {
                _StartTime = value;
                RaisePropertyChanged("StartTime");
            }
        }
        /// <summary>
        /// 搜索条件结束时间
        /// </summary>
        private string _EndTime;
        /// <summary>
        /// 搜索条件结束时间
        /// </summary>
        public string EndTime
        {
            get { return _EndTime; }
            set
            {
                _EndTime = value;
                RaisePropertyChanged("EndTime");
            }
        }

        #endregion 快速筛选搜索条件的数据
        /// <summary>
        /// 构造方法
        /// </summary>
        public CastingRecordPageViewModel()
        {
            InitCommand();
            //刷新数据
            Messenger.Default.Register<string>(this, "CastingRecordPageRefurbish", (message) =>
            {
                InitCondition();
            });
            InitCondition();
            //获取传递过来的时间参数开始
            Messenger.Default.Register<string>(this, "SetStart", (message) =>
            {
                StartTime = message;
                InitCondition();
            });
            //获取传递过来的时间参数结束
            Messenger.Default.Register<string>(this, "SetEnd", (message) =>
            {
                EndTime = message;
                InitCondition();
            });
        }
        /// <summary>
        /// 初始化条件数据
        /// </summary>
        private void InitCondition()
        {
            //加载最近使用铸造编号条件列表
            string sql = @"SELECT TOP 5
	                            b.CastNumber
                            FROM RecipeTable a
                            INNER JOIN CastNumberTable b ON a.RecipeId = b.RecipeId
                            GROUP BY b.CastNumber,b.CastStartTime
                            ORDER BY b.CastStartTime DESC";
            LastUsedCastingNumbers = DapperToolsThisSystem.QueryOc<RecipeTable>(sql);
            //加载配方名称条件列表
            sql = @"SELECT TOP 5
	                            a.RecipeName as RecipeNameStr
                            FROM RecipeTable a
                            INNER JOIN CastNumberTable b ON a.RecipeId = b.RecipeId
                            GROUP BY a.RecipeName,b.CastStartTime
                            ORDER BY b.CastStartTime DESC";
            RecentlyUsedRecipes = DapperToolsThisSystem.QueryOc<RecipeTable>(sql);
            //加载工艺条件列表
            sql = @"SELECT TOP 5
	                            a.MSC_MoldTech
                            FROM RecipeTable a
                            INNER JOIN CastNumberTable b ON a.RecipeId = b.RecipeId
                            GROUP BY a.MSC_MoldTech,b.CastStartTime
                            ORDER BY b.CastStartTime DESC";
            MoldTechs = DapperToolsThisSystem.QueryOc<RecipeTable>(sql);
            QueryDataCollection();
        }
        /// <summary>
        /// 搜索配方列表数据
        /// </summary>
        private void QueryDataCollection()
        {
            string baseSearchSql = @"SELECT ROW_NUMBER() OVER (
                                                                ORDER BY b.CastStartTime DESC
                                                               ) as rownumber,
                                             a.RecipeId
                                            ,a.RecipeName as RecipeNameStr
                                            ,a.FRN_FRNBATHTEMPHILIM
                                            ,a.FRN_FRNBATHTEMPLOLIM
                                            ,a.FRN_FRNBATHTEMPSP
                                            ,a.FRN_FRNTILTBACKOFFSET
                                            ,a.GAS_BEGINRAMPDOWN
                                            ,a.GAS_ENDRAMPDOWN
                                            ,a.GAS_IDLECASTGAS
                                            ,a.GAS_RUNCASTGAS
                                            ,a.GAS_STARTCASTGAS
                                            ,a.GAS_STARTCASTGASLEN
                                            ,a.JET_OFFPRESLEN
                                            ,a.JET_OFFPRESLENEND
                                            ,a.JET_PRES1
                                            ,a.JET_PRES1END
                                            ,a.JET_PRES2
                                            ,a.JET_PRES2END
                                            ,a.JET_PRESLEN1
                                            ,a.JET_PRESLEN1END
                                            ,a.JET_PRESLEN2
                                            ,a.JET_PRESLEN2END
                                            ,a.JET_STARTPRES
                                            ,a.JET_STARTPRESEND
                                            ,a.LEN_CASTLENGTH
                                            ,a.LUB_LEN2
                                            ,a.LUB_LEN3
                                            ,a.LUB_LEN4
                                            ,a.LUB_PULSELUBECYCTIME
                                            ,a.LUB_PULSELUBECYCTIME2
                                            ,a.LUB_PULSELUBECYCTIME3
                                            ,a.LUB_PULSELUBECYCTIME4
                                            ,a.LUB_PULSELUBEOFFTIME
                                            ,a.LUB_PULSELUBEONTIME
                                            ,a.LUB_PULSELUBEPRESFIRE
                                            ,a.LUB_PULSELUBEPRESHOLD
                                            ,a.LUB_PULSELUBEPRESRESET
                                            ,a.LUB_PULSELUBEPULSES
                                            ,a.LUB_PULSELUBEPULSES2
                                            ,a.LUB_PULSELUBEPULSES3
                                            ,a.LUB_PULSELUBEPULSES4
                                            ,a.MLC_FILLRATE1
                                            ,a.MLC_FILLRATE1STARTLEVEL
                                            ,a.MLC_FILLRATE2
                                            ,a.MLC_FILLRATE2STARTLEVEL
                                            ,a.MLC_FILLRATE3
                                            ,a.MLC_FILLRATE3STARTLEVEL
                                            ,a.MLC_FILLRATE4
                                            ,a.MLC_FILLRATE4STARTLEVEL
                                            ,a.MLC_FILLRATESTART
                                            ,a.MLC_MOLDLVL1
                                            ,a.MLC_MOLDLVL1LEN
                                            ,a.MLC_MOLDLVL2
                                            ,a.MLC_MOLDLVL2LEN
                                            ,a.MLC_MOLDLVL3
                                            ,a.MLC_MOLDLVL3LEN
                                            ,a.MLC_MOLDLVL4
                                            ,a.MLC_MOLDLVL4LEN
                                            ,a.MLC_MOLDLVL5
                                            ,a.MLC_MOLDLVL5LEN
                                            ,a.MLC_MOLDLVL6
                                            ,a.MLC_MOLDLVL6LEN
                                            ,a.MLC_MOLDLVL7
                                            ,a.MLC_MOLDLVL7LEN
                                            ,a.MLC_MOLDLVL8
                                            ,a.MLC_MOLDLVL8LEN
                                            ,a.MLC_MOLDLVL9
                                            ,a.MLC_MOLDLVL9LEN
                                            ,a.MLC_MOLDLVLSTART
                                            ,a.MLC_PINPRESET1
                                            ,a.MLC_PINPRESET2
                                            ,a.MLC_PINPRESET3
                                            ,a.MLC_PINPRESET4
                                            ,a.MLC_PINPRESET5
                                            ,a.MLC_PINPRESET6
                                            ,a.MLC_PINPRESET7
                                            ,a.MLC_PINPRESET8
                                            ,a.MLC_PINPRESET9
                                            ,a.MLC_PINPRESET10
                                            ,a.MLC_PINPRESET11
                                            ,a.MLC_PINPRESET12
                                            ,a.MLC_PINPRESET13
                                            ,a.MLC_PINPRESET14
                                            ,a.MLC_PINPRESET15
                                            ,a.MLC_UNCNTRLDFILLDIST
                                            ,a.MTL_DEGASTEMP
                                            ,a.ROD_RODSPEED
                                            ,a.SPD_CASTSPD1
                                            ,a.SPD_CASTSPD1LEN
                                            ,a.SPD_CASTSPD2
                                            ,a.SPD_CASTSPD2LEN
                                            ,a.SPD_CASTSPD3
                                            ,a.SPD_CASTSPD3LEN
                                            ,a.SPD_CASTSPD4
                                            ,a.SPD_CASTSPD4LEN
                                            ,a.SPD_CASTSPD5
                                            ,a.SPD_CASTSPD5LEN
                                            ,a.SPD_CASTSPD6
                                            ,a.SPD_CASTSPD6LEN
                                            ,a.SPD_CASTSPD7
                                            ,a.SPD_CASTSPD7LEN
                                            ,a.SPD_CASTSPD8
                                            ,a.SPD_CASTSPD8LEN
                                            ,a.SPD_CASTSPD9
                                            ,a.SPD_CASTSPD9LEN
                                            ,a.SPD_CASTSPDEND
                                            ,a.SPD_CASTSPDSTART
                                            ,a.SPD_PLTNHOLDTIME
                                            ,a.TGH_DAMPULLSP1
                                            ,a.TGH_DAMPULLSP2
                                            ,a.TGH_TROUGHTEMP
                                            ,a.WIP_WiperOnLen
                                            ,a.WIP_WiperPosition
                                            ,a.WIP_WIPPOS1
                                            ,a.WIP_WIPPOS1LEN
                                            ,a.WIP_WIPPOS2
                                            ,a.WIP_WIPPOS2LEN
                                            ,a.WIP_WIPPOS3
                                            ,a.WIP_WIPPOS3LEN
                                            ,a.WIP_WIPPOS4
                                            ,a.WIP_WIPPOS4LEN
                                            ,a.WIP_WIPPOS5
                                            ,a.WIP_WIPPOS5LEN
                                            ,a.WIP_WIPPOS6
                                            ,a.WIP_WIPPOS6LEN
                                            ,a.WIP_WIPPOS7
                                            ,a.WIP_WIPPOS7LEN
                                            ,a.WIP_WIPPOS8
                                            ,a.WIP_WIPPOS8LEN
                                            ,a.WIP_WIPPOS9
                                            ,a.WIP_WIPPOS9LEN
                                            ,a.WIP_WIPPOSEND
                                            ,a.WIP_WIPPOSSTART
                                            ,a.WTR_WATEROFFDELAY
                                            ,a.WTR_WTRFLOW1
                                            ,a.WTR_WTRFLOW1LEN
                                            ,a.WTR_WTRFLOW2
                                            ,a.WTR_WTRFLOW2LEN
                                            ,a.WTR_WTRFLOW3
                                            ,a.WTR_WTRFLOW3LEN
                                            ,a.WTR_WTRFLOW4
                                            ,a.WTR_WTRFLOW4LEN
                                            ,a.WTR_WTRFLOW5
                                            ,a.WTR_WTRFLOW5LEN
                                            ,a.WTR_WTRFLOW6
                                            ,a.WTR_WTRFLOW6LEN
                                            ,a.WTR_WTRFLOW7
                                            ,a.WTR_WTRFLOW7LEN
                                            ,a.WTR_WTRFLOW8
                                            ,a.WTR_WTRFLOW8LEN
                                            ,a.WTR_WTRFLOW9
                                            ,a.WTR_WTRFLOW9LEN
                                            ,a.WTR_WTRFLOWEND
                                            ,a.WTR_WTRFLOWSTART
                                            ,a.WTR_WTRTEMPHILIMIT
                                            ,a.WTR_WTRTEMPLOLIMIT
                                            ,a.MSC_COOLDOWNTIME
                                            ,a.MSC_MOLDDIAMETER
                                            ,a.MSC_MOLDHEIGHT
                                            ,a.MSC_MOLDTHICKNESS
                                            ,a.MSC_MOLDWIDTH
                                            ,a.MSC_RECNAME
                                            ,a.MSC_NUMBERMOLDS
                                            ,a.MSC_REV
                                            ,a.MSC_UserDef1
                                            ,a.MSC_UserDef2
                                            ,a.MSC_UserDef3
                                            ,a.MSC_UserDef4
                                            ,a.MSC_UserDef5
                                            ,a.MSC_UserDef6
                                            ,a.MSC_UserDef7
                                            ,a.MSC_UserDef8
                                            ,a.MSC_UserDef9
                                            ,a.MSC_UserDef10
                                            ,a.MSC_Description
                                            ,a.MSC_Alloy
                                            ,a.MSC_MoldTech
                                            ,a.MSC_DateCreated
                                            ,a.MSC_DateLastUsed
                                            ,a.MSC_CreatedBy
                                            ,a.MSC_Type
                                            ,b.CastNumber
                                            ,CONVERT(VARCHAR(255),b.CastStartTime,120) as CastStartTimeStr
                                            ,CONVERT(VARCHAR(255),b.CastStopTime,120) as CastStopTimeStr
                                     FROM RecipeTable a
                                     INNER JOIN CastNumberTable b ON a.RecipeId = b.RecipeId
                                     WHERE 1 = 1
                                     " + GetConditionalSql();

            string pageingSql = @"SELECT TOP " + itemCount + @" * 
                                  FROM (" + baseSearchSql + @") temp_row
                                  WHERE rownumber > ((" + CurrentPageNumber + @" - 1 ) * " + itemCount + @" ); ";


            DataCollection = DapperToolsThisSystem.QueryOc<RecipeTable>(pageingSql);
            //总数据量
            TotalDataCount = DapperToolsThisSystem.GetCount(baseSearchSql);

            for (int i = 0; i < DataCollection.Count; i++)
            {
                DataCollection[i].OrderNo = (i + 1);
            }
        }
        /// <summary>
        /// 获取条件语句
        /// </summary>
        /// <returns></returns>
        private string GetConditionalSql()
        {
            string ConditionalSql = "";
            if (CastingNumberConditions.Count > 0)
            {
                ConditionalSql += " AND (";
                for (int i = 0; i < CastingNumberConditions.Count; i++)
                {
                    if (i > 0)
                    {
                        ConditionalSql += " OR b.CastNumber = '" + CastingNumberConditions[i] + "'";
                    }
                    else
                    {
                        ConditionalSql += " b.CastNumber = '" + CastingNumberConditions[i] + "'";
                    }

                }
                ConditionalSql += " ) ";
            }

            if (RecipeNameConditions.Count > 0)
            {
                ConditionalSql += " AND (";
                for (int i = 0; i < RecipeNameConditions.Count; i++)
                {
                    if (i > 0)
                    {
                        ConditionalSql += " OR a.RecipeName = '" + RecipeNameConditions[i] + "'";
                    }
                    else
                    {
                        ConditionalSql += " a.RecipeName = '" + RecipeNameConditions[i] + "'";
                    }

                }
                ConditionalSql += " ) ";

            }
            if (MoldTechConditions.Count > 0)
            {
                ConditionalSql += " AND (";
                for (int i = 0; i < MoldTechConditions.Count; i++)
                {
                    if (i > 0)
                    {
                        ConditionalSql += " OR a.MSC_MoldTech = '" + MoldTechConditions[i] + "'";
                    }
                    else
                    {
                        ConditionalSql += " a.MSC_MoldTech = '" + MoldTechConditions[i] + "'";
                    }

                }
                ConditionalSql += " ) ";
            }
            if (TypeConditions.Count > 0)
            {
                ConditionalSql += " AND (";
                for (int i = 0; i < TypeConditions.Count; i++)
                {
                    if (i > 0)
                    {
                        ConditionalSql += " OR MSC_Type = '" + TypeConditions[i] + "'";
                    }
                    else
                    {
                        ConditionalSql += " MSC_Type = '" + TypeConditions[i] + "'";
                    }
                }
                ConditionalSql += " ) ";
            }

            if (CastNumber != null && CastNumber.Trim() != "")
            {
                ConditionalSql += " AND CastNumber LIKE '%" + CastNumber + "%'";
            }
            if (StartTime != null && StartTime.Trim() != "")
            {
                ConditionalSql += " AND CastStartTime  >= '" + StartTime + "'";
            }
            if (EndTime != null && EndTime.Trim() != "")
            {
                ConditionalSql += " AND CastStartTime < '" + EndTime + "'";
            }
            return ConditionalSql;
        }

    }
}
